package layeredpanel.Layers;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.OverlayLayout;
import javax.swing.WindowConstants;
import javax.swing.text.JTextComponent;

import swinglayout.GroupLayout;
import swinglayout.LayoutStyle;

/**
 * 
 * @author Romain Guy
 */
public class ApplicationFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Creates new form ApplicationFrame */
	public ApplicationFrame() {
		initComponents();

		addLayeredValidator();
		addValidations();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc=" Generated Code
	// ">//GEN-BEGIN:initComponents
	private void initComponents() {
		JLabel jLabel1;
		JLabel jLabel2;
		JLabel jLabel3;
		JLabel jLabel4;
		JLabel jLabel5;
		JList jList1;
		JScrollPane jScrollPane1;
		JScrollPane jScrollPane2;

		jScrollPane1 = new JScrollPane();
		jList1 = new JList();
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jLabel3 = new JLabel();
		jLabel4 = new JLabel();
		jLabel5 = new JLabel();
		firstName = new JTextField();
		lastName = new JTextField();
		phoneNumber = new JTextField();
		email = new JTextField();
		jScrollPane2 = new JScrollPane();
		address = new JTextArea();

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("Layered Panes");
		jList1.setModel(new AbstractListModel() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			String[] strings = { "Jeff Dinkins", "Richard Bair", "Amy Fowler",
					"Scott Violet", "Hans Muller", "Chris Campbell",
					"Chet Haase" };

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		jScrollPane1.setViewportView(jList1);

		jLabel1.setText("First Name");

		jLabel2.setText("Last Name");

		jLabel3.setText("Phone");

		jLabel4.setText("Email");

		jLabel5.setText("Address");

		address.setColumns(15);
		address.setRows(5);
		jScrollPane2.setViewportView(address);

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap().add(
						jScrollPane1, GroupLayout.PREFERRED_SIZE, 152,
						GroupLayout.PREFERRED_SIZE).addPreferredGap(
						LayoutStyle.RELATED).add(
						layout.createParallelGroup(GroupLayout.LEADING).add(
								GroupLayout.TRAILING, jLabel5).add(
								GroupLayout.TRAILING, jLabel4).add(
								GroupLayout.TRAILING, jLabel3).add(
								GroupLayout.TRAILING, jLabel2).add(
								GroupLayout.TRAILING, jLabel1))
						.addPreferredGap(LayoutStyle.RELATED).add(
								layout.createParallelGroup(GroupLayout.LEADING)
										.add(firstName,
												GroupLayout.DEFAULT_SIZE, 209,
												Short.MAX_VALUE).add(lastName,
												GroupLayout.DEFAULT_SIZE, 209,
												Short.MAX_VALUE).add(
												phoneNumber,
												GroupLayout.DEFAULT_SIZE, 209,
												Short.MAX_VALUE).add(email,
												GroupLayout.DEFAULT_SIZE, 209,
												Short.MAX_VALUE).add(
												jScrollPane2,
												GroupLayout.DEFAULT_SIZE, 209,
												Short.MAX_VALUE))
						.addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				layout
																						.createParallelGroup(
																								GroupLayout.BASELINE)
																						.add(
																								jLabel1)
																						.add(
																								firstName,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				LayoutStyle.RELATED)
																		.add(
																				layout
																						.createParallelGroup(
																								GroupLayout.BASELINE)
																						.add(
																								jLabel2)
																						.add(
																								lastName,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				LayoutStyle.RELATED)
																		.add(
																				layout
																						.createParallelGroup(
																								GroupLayout.BASELINE)
																						.add(
																								jLabel3)
																						.add(
																								phoneNumber,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				LayoutStyle.RELATED)
																		.add(
																				layout
																						.createParallelGroup(
																								GroupLayout.BASELINE)
																						.add(
																								jLabel4)
																						.add(
																								email,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				LayoutStyle.RELATED)
																		.add(
																				layout
																						.createParallelGroup(
																								GroupLayout.LEADING)
																						.add(
																								jLabel5)
																						.add(
																								jScrollPane2))
																		.add(
																				6,
																				6,
																				6))
														.add(
																jScrollPane1,
																GroupLayout.DEFAULT_SIZE,
																210,
																Short.MAX_VALUE))
										.addContainerGap()));
		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 489) / 2, (screenSize.height - 266) / 2,
				489, 266);
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new ApplicationFrame().setVisible(true);
			}
		});
	}

	private void addLayeredValidator() {
		validator = new Validator();

		JLayeredPane layeredPane = getRootPane().getLayeredPane();
		layeredPane.setLayout(new OverlayLayout(layeredPane));
		layeredPane.add(validator, (Integer) (JLayeredPane.DEFAULT_LAYER + 50));
		// validator.setBounds(0, 0, getWidth(), getHeight());
	}

	private void addValidations() {
		addValidationForText(address);
		addValidationForText(firstName);
		addValidationForText(lastName);
		addValidationForNumber(phoneNumber);
		addValidationForEmail(email);
	}

	private void addValidationForText(JTextComponent field) {
		field.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent focusEvent) {
			}

			public void focusLost(FocusEvent focusEvent) {
				JTextComponent field = (JTextComponent) focusEvent
						.getComponent();
				String text = field.getText();

				if (text.matches("[-A-Za-z ]*")) {
					validator.addWarning(field);
				} else {
					validator.removeWarning(field);
				}
			}
		});
	}

	private void addValidationForNumber(JTextComponent field) {
		field.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent focusEvent) {
			}

			public void focusLost(FocusEvent focusEvent) {
				JTextField field = (JTextField) focusEvent.getComponent();
				String text = field.getText();

				if (text.matches("[-()0-9 ]*")) {
					validator.addWarning(field);
				} else {
					validator.removeWarning(field);
				}
			}
		});
	}

	private void addValidationForEmail(JTextComponent field) {
		field.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent focusEvent) {
			}

			public void focusLost(FocusEvent focusEvent) {
				JTextComponent field = (JTextComponent) focusEvent
						.getComponent();
				String text = field.getText();

				if (text.matches("[^@]+@([^.]+\\.)+[^.]+")) {
					validator.addWarning(field);
				} else {
					validator.removeWarning(field);
				}
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JTextArea address;
	private JTextField email;
	private JTextField firstName;
	private JTextField lastName;
	private JTextField phoneNumber;
	// End of variables declaration//GEN-END:variables

	private Validator validator;

}
